package com.dhl.deliver.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dhl.deliver.entity.Address;
import com.dhl.deliver.entity.Product;
import com.dhl.deliver.entity.Shop;
import com.dhl.deliver.service.IProductService;
import com.dhl.deliver.service.IShopService;
import com.dhl.deliver.utils.Result;
import com.dhl.deliver.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dhl
 * @since 2020-07-15
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    IShopService service;

    @Autowired
    IProductService productService;

    @ApiOperation("当前登录商家创建或者修改商铺")
    @PostMapping("/creatShop")
    public Result creatShop(HttpServletRequest servletRequest,String shopName,String shopIntroduce,String shopAddress){
        String user_id=String.valueOf(servletRequest.getSession().getAttribute("id"));
        Wrapper<Shop> wrapper=new QueryWrapper<Shop>().eq("user_id",user_id);
        Shop shop=service.getOne(wrapper);
        System.out.println(shop);
        if(shop==null){
            System.out.println("创建商铺");
            shop=new Shop();
            shop.setUserId(user_id);
            shop.setName(shopName);
            shop.setIntroduce(shopIntroduce);
            shop.setAddress(shopAddress);
            if(service.save(shop)){
                return ResultUtil.success();
            }
            return ResultUtil.error(500,"创建失败");
        }
        else {
            System.out.println("修改商铺");
            shop.setName(shopName);
            shop.setIntroduce(shopIntroduce);
            shop.setAddress(shopAddress);
            int id=shop.getId();
            service.updateById(shop);
            return ResultUtil.success();
        }
    }

    @ApiOperation("获取当前登录商家的商铺")
    @GetMapping("/getShops")
    public Result getShops(HttpServletRequest servletRequest){
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<Shop>().eq("user_id", servletRequest.getSession().getAttribute("id"));
        Shop shop=service.getOne(queryWrapper);
        if(shop==null){
            return ResultUtil.error(500,"该商家还没有商铺");
        }
//        QueryWrapper<Product> listWrapper = new QueryWrapper<Product>().eq("shop_id", shop.getId());
//        List<Product> productList=productService.list(listWrapper);
//        Map<Shop,List<Product>>listHashMap=new HashMap<>();
//        listHashMap.put(shop,productList);
        Result result = ResultUtil.success(shop);
        return result;
    }

    @ApiOperation("根据商铺的id，更改商铺的名称")
    @PostMapping("modifyShopName")
    public Result modifyShopName(String id,String name){
        Shop shop=service.getById(id);
        shop.setName(name);
        if(service.updateById(shop)){
            return ResultUtil.error(500,"修改失败");
        };
        return ResultUtil.success();
    }

    @ApiOperation("根据商铺id，对商铺的简介进行修改")
    @PostMapping("modifyShopIntroduce")
    public Result modifyShopIntroduce(String id,String introduce){
        Shop shop=service.getById(id);
        shop.setIntroduce(introduce);
        if(service.updateById(shop)){
            return ResultUtil.error(500,"修改失败");
        };
        return ResultUtil.success();
    }

    @ApiOperation("根据商铺id，对商铺的所在地址进行修改")
    @PostMapping("modifyShopAddress")
    public Result modifyShopAddress(String id,String Address){
        Shop shop=service.getById(id);
        shop.setAddress(Address);
        if(service.updateById(shop)){
            return ResultUtil.error(500,"修改失败");
        };
        return ResultUtil.success();
    }

    @ApiOperation("获取所有商铺")
    @PostMapping("getAllShops")
    public Result getAllShops(){
        List<Shop> shops=service.list();
//        Map<Shop,List<Product>> allInfoAboutShop=new HashMap<>();
//        for(Shop shop:shops){
//            QueryWrapper<Product> listWrapper = new QueryWrapper<Product>().eq("shop_id", shop.getId());
//            List<Product> productList=productService.list(listWrapper);
//            allInfoAboutShop.put(shop,productList);
//        }
        return ResultUtil.success(shops);
    }
}
